package com.cf.base.comm.module;

import android.content.Context;
import android.content.res.Configuration;


import com.cf.base.comm.module.tools.ClassUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author yangji
 */
public class ModuleApplicationManager extends BaseModuleApplication {

    private List<IBaseApplication> applicationList;

    private void loadModuleApplication() {
        applicationList = new ArrayList<>();
        Set<String> adapterMap = null;
        try {
            adapterMap = ClassUtils.getFileNameByPackageName(getApplicationContext(), "com.cf.core");
            for (String className : adapterMap) {
                Class cls = Class.forName(className);
                if (IBaseApplication.class.isAssignableFrom(cls)) {
                    IBaseApplication application = (IBaseApplication) cls.newInstance();
                    application.attachBaseContext(getApplicationContext());
                    applicationList.add(application);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate() {
        loadModuleApplication();
        for (IBaseApplication application : applicationList) {
            application.onCreate();
        }
    }

    @Override
    public void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        if (applicationList != null) {
            for (IBaseApplication application : applicationList) {
                application.attachBaseContext(base);
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (applicationList != null) {
            for (IBaseApplication application : applicationList) {
                application.onConfigurationChanged(newConfig);
            }
        }
    }
}
